home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
NetNews Offline 2
/
NetNews Offline Volume 2.iso
/
news
/
comp
/
std
/
c
/
48
< prev
next >
Wrap
Internet Message Format
|
1996-08-06
|
2KB
Path: news.cs.ucla.edu!twinsun!not-for-mail
From: eggert@twinsun.com (Paul Eggert)
Newsgroups: comp.std.c
Subject: Re: Undefined result vs. int's holding undefined values.
Date: 7 Jan 1996 18:20:39 -0800
Organization: Twin Sun Inc, El Segundo, CA, USA
Message-ID: <4cpv1n$apm@der.twinsun.com>
References: <4ck70b$rd7@news.informix.com> <4ckms5$rd7@news.informix.com> <4cmg0s$1mb@der.twinsun.com> <oZA8wQ9ytpjN084yn@csn.net>
NNTP-Posting-Host: der.twinsun.com
thads@csn.net (Thad Smith) writes:
> it is possible to rewrite the function so that it doesn't
> invoke undefined behavior
Yes. But it slows things down measurably for an interpreter to use
such a clumsy method for integer overflow detection. The (x + y < x)
!= (y < 0) method requires a comparison, an exclusive OR of the
comparison result bit with a sign bit, and a conditional branch.
The method you suggested requires a comparison, a conditional branch, a
subtraction, another comparison, and another conditional branch.
Since this is comp.std.c, I'll mention that the (x + y < x) != (y < 0) method
and Daniel Wood's example both port to any C implementation
claiming conformance to LIA-1 (ISO/IEC 10967-1:1994(E),
`Information technology -- Language independent arithmetic -- Part 1:
Integer floating point arithmetic' <ftp://crl.dec.com/pub/misc/lia-1-dis.ps.Z>)
if INT_MODULO is true (which is the only plausible value for INT_MODULO
for real-world C compilers). I doubt whether Wood's compiler vendor
claims LIA-1 conformance today, but this may become more important in
the future, and perhaps Wood can point his vendor at LIA-1 and say that
it's time to read the handwriting on the wall.